package org.ichow.question.plugins
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
	import org.ichow.question.Question;
	
	/**
	 * ...
	 * 题目插件基类
	 * @author Minos
	 */
	public class QuestionPlugin
	{
		//API
		public static const VERSION : Number = 1.0;
		public static const API : Number = 1.0;
		//插件类型
		public var propName : String;
		//素材
		protected var skin : DisplayObjectContainer;
		//数据
		protected var data : Object;
		//分数
		protected var tScore : int;
		//得分
		protected var _score : Number;
		//选项个数
		protected var options : int;
		//答案
		protected var answer : Array;
		//选项标识符
		protected var optionName : String = "a_$_obj";
		
		public function QuestionPlugin()
		{
			//构造
		}
		
		/**
		 * 初始化插件
		 */
		protected function init() : void
		{
			tScore = int( getData( "score" ) );
			options = int( getData( "options" ) );
			answer = getAnswer();
			_score = 0;
		}
		
		/**
		 * 获取参数
		 * @param	v
		 * @return
		 */
		protected final function getData( v : String ) : Object
		{
			if ( data[ v ] )
				return data[ v ];
			return null;
		}
		
		/**
		 * 检查是否有该参数
		 * @param	v
		 * @return
		 */
		protected final function hasData( v : String ) : Boolean
		{
			return data[ v ] ? true : false;
		}
		
		/**
		 * 获取素材中的可视对象
		 * @param	name
		 * @return
		 */
		protected final function getChild( name : String ) : DisplayObject
		{
			var d : DisplayObject = null;
			if ( skin && skin is DisplayObjectContainer )
				d = skin.getChildByName( name );
			return d;
		}
		
		/**
		 * 获取答案数组（深复制）
		 * @return
		 */
		protected function getAnswer() : Array
		{
			var ba : ByteArray = new ByteArray();
			ba.writeObject( getData( "answer" ) );
			ba.position = 0;
			return ba.readObject() as Array;
		}
		
		/**
		 * 绑定素材和数据
		 * @param	skin
		 * @param	data
		 */
		public function bind( skin : DisplayObjectContainer , data : Object = null ) : void
		{
			this.data = data;
			this.skin = skin;
			init();
		}
		
		/**
		 * 启动
		 */
		public function active() : void
		{
			addListeners();
		}
		
		/**
		 * 取消
		 */
		public function cancel() : void
		{
			removeListeners();
		}
		
		/**
		 * 重置
		 */
		public function reset() : void
		{
			//to protected
		}
		
		/**
		 * 添加侦听
		 */
		protected function addListeners() : void
		{
			//to protected
		}
		
		/**
		 * 移除侦听
		 */
		protected function removeListeners() : void
		{
			//to protected
		}
		
		/**
		 * 检查
		 * @return
		 */
		public function check() : Boolean
		{
			return false;
		}
		
		/**
		 * 获取得分
		 */
		public function get score() : Number
		{
			return _score;
		}
		
		/**
		 * 启动插件
		 * @param	plugins
		 * @return
		 */
		public static function activate( plugins : Array ) : Boolean
		{
			var i : int = plugins.length , instance : Object;
			while ( i-- )
			{
				if ( plugins[ i ].hasOwnProperty( "API" ) )
				{
					instance = new ( plugins[ i ] as Class )();
					Question.activatedPlugins[ instance.propName ] = plugins[ i ];
				}
			}
			return true;
		}
	
	}

}